package org.linlinjava.litemall.wxserver.web;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.service.LitemallAdService;
import org.linlinjava.litemall.db.service.LitemallMechanismCategoryService;
import org.linlinjava.litemall.db.service.LitemallMechanismGoodsService;
import org.linlinjava.litemall.db.service.LitemallMechanismService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author FistJie
 */
@RestController
@RequestMapping("/wxs/service")
public class WxHomeController {

	private LitemallMechanismCategoryService litemallMechanismCategoryService;

	private LitemallMechanismService mechanismService;

	private LitemallAdService adService;

	private LitemallMechanismGoodsService mechanismGoodsService;

	private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(30);

	private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

	private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(8, 100, 2000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);

	public WxHomeController(
		LitemallMechanismCategoryService litemallMechanismCategoryService,
		LitemallMechanismService mechanismService,
		LitemallAdService adService,
		LitemallMechanismGoodsService mechanismGoodsService) {
		this.litemallMechanismCategoryService = litemallMechanismCategoryService;
		this.mechanismService = mechanismService;
		this.adService = adService;
		this.mechanismGoodsService = mechanismGoodsService;
	}

	/**
	 * app首页
	 *
	 * @return app首页相关信息
	 * 成功则
	 * {
	 * errno: 0,
	 * errmsg: '成功',
	 * data:
	 * {
	 * banner: xxx,
	 * channel: xxx,
	 * newGoodsList: xxx,
	 * hotGoodsList: xxx,
	 * topicList: xxx,
	 * floorGoodsList: xxx
	 * }
	 * }
	 * 失败则 { errno: XXX, errmsg: XXX }
	 */
	@GetMapping("/index")
	public Object index(String longitude, String latitude) {
		// 机构分类
		Callable<List> mechanismCategoryListCallable = () -> litemallMechanismCategoryService.selectAll();

		//附近的机构
		Callable<List> mechanismListCallable = () -> mechanismService.searchMechanism(longitude, latitude, "");

		//附近广告
		Callable<List> mechanismAdListCallable = () -> {
			List<Map> mechanismList = adService.selectMechanismAd(longitude, latitude);
			if (mechanismList.size() == 0) {
				return adService.queryIndex();
			} else {
				return mechanismList;
			}
		};

		//Top3
		Callable<List> mechanismGoodsTop3ListCallable = () ->  mechanismGoodsService.queryTop3(longitude, latitude);

		FutureTask<List> mechanismCategoryListTask = new FutureTask<>(mechanismCategoryListCallable);
		FutureTask<List> mechanismListTask = new FutureTask<>(mechanismListCallable);
		FutureTask<List> mechanismAdListTask = new FutureTask<>(mechanismAdListCallable);
		FutureTask<List> mechanismGoodsTop3ListTask = new FutureTask<>(mechanismGoodsTop3ListCallable);

		executorService.submit(mechanismCategoryListTask);
		executorService.submit(mechanismListTask);
		executorService.submit(mechanismAdListTask);
		executorService.submit(mechanismGoodsTop3ListTask);

		Map<String, Object> data = new HashMap<>(16);
		try {
			data.put("mcategoryList", mechanismCategoryListTask.get());
			data.put("mechanismList", mechanismListTask.get());
			data.put("banner", mechanismAdListTask.get());
			data.put("top3", mechanismGoodsTop3ListTask.get());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok(data);
	}
}


